#define _events_included 1

#if !defined _gam_cmd_included
	#include <gam/cmd>
#endif
#if !defined _gam_level_powers_included
	#include <gam/level_powers>
#endif


public CTwin(){
	gCtWin = true;
}

public RoundStartEvent(){
	gCtWin = false;
	gRoundTime = get_gametime();
}
stock addPlayerFrags(id, victim, wpn, hs){
	if(id != victim && isPlayer(id) && isPlayer(victim)) {		
		if(0<wpn<WEAPONS){
			gPlayerInfo[id][wpn][REQ_KILLS_ROUND]++;
			gPlayerInfo[id][wpn][REQ_KILLS_MAP]++;
			gPlayerInfo[id][wpn][REQ_KILLS_TOTAL]++;
			new iXp = 0;
			
			if(hs) {
				iXp = get_pcvar_num(gCvarXpHs);
				gPlayerInfo[id][wpn][REQ_HS_ROUND]++;
				gPlayerInfo[id][wpn][REQ_HS_MAP]++;
				gPlayerInfo[id][wpn][REQ_HS_TOTAL]++;
			} else {
				iXp = get_pcvar_num(gCvarXpKill);
			}
			if(gPlayerRank[id] < gPlayerRank[victim]){
				iXp += get_pcvar_num(gCvarXpKillHigherRank);
			}
			gPlayerXp[id][T_MAP] += iXp;
			gPlayerXp[id][T_ROUND] += iXp;
			gPlayerXp[id][T_TOTAL] += iXp;
			checkPlayerNextLevel4Weapon(id, wpn);
			gPlayerSaveNeeded[id] = true;
		}
	}
}

stock checkPlayerRank(id){
	new rank= gPlayerRank[id];
	if(rank != RANK_GENERAL_OF_THE_ARMY){
		rank++;
		if(gPlayerXp[id][T_TOTAL] >= RANK_REQ[rank]){
			setPlayerRank(id, rank);
		}
	}
}

stock setPlayerRank(id, rank){
	gPlayerRank[id] = rank;
	new rankName[32];
	getRankName(id, rank, rankName, charsmax(rankName));
	ColorChat(id, BLUE, "^0x4%L ^x03", id, "PROMOTED_RANK", rankName);
}

public RoundEndEvent() {
	if(!isActive())
		return PLUGIN_CONTINUE;
		
	new players[32];
	new num;
	new player;
	new i;
	new CsTeams:team;
	get_players(players, num, "h");
	if(isEnoughPlayers()){
		new xpAlive = get_pcvar_num(gCvarXpRoundWinAlive);
		new xpDead = get_pcvar_num(gCvarXpRoundWinDead);
		new xpBest = get_pcvar_num(gCvarXpBestInRound);
		
		if(xpAlive || xpDead){
			for(new i=0;i<num;i++){
				player = players[i];
				team  = cs_get_user_team(player) ;
				if( (gCtWin && team == CS_TEAM_CT) || (!gCtWin && team == CS_TEAM_T) ){
					if(is_user_alive(player)){
						gPlayerXp[player][T_ROUND] += xpAlive;
						gPlayerXp[player][T_MAP] += xpAlive;
						gPlayerXp[player][T_TOTAL] += xpAlive;
						gPlayerSaveNeeded[player] = true;
					}
					else {
						gPlayerXp[player][T_ROUND] += xpDead;
						gPlayerXp[player][T_MAP] += xpDead;
						gPlayerXp[player][T_TOTAL] += xpDead;
						gPlayerSaveNeeded[player] = true;
					}
				}
			}
		}
		if(xpBest) {
			player = getRoundBestPlayer(players, num);
			if(player) {
				gPlayerXp[player][T_MAP] += xpBest;
				gPlayerXp[player][T_TOTAL] += xpBest;
				gPlayerSaveNeeded[player] = true;
				new name[MAX_KEY_LEN];
				get_user_name(player, name, MAX_KEY_LEN -1);
				set_hudmessage(255, 0, 0, -1.0, 0.15, 0, 6.0, 10.0, 0.1, 0.2, 5);
				ShowSyncHudMsg(0, gSyncHudObject, "%L", LANG_PLAYER, "BEST_IN_ROUND", name);
				ColorChat(player, RED, "^x04%L ^x03%d XP", player, "BEST_IN_ROUND_REWORD", xpBest);
			}
		}
	}

	for(i=0;i<num;i++) {
		player = players[i];
		clearUserRoundXp(player);
		if(gPlayerSaveNeeded[player]) {
			savePlayer(player);
			gPlayerSaveNeeded[player] = false;
		}
	}
		
	return PLUGIN_CONTINUE;
}




public DeathMsgEvent() {
	if(!isActive())
		return PLUGIN_CONTINUE;
		
	new id = read_data(1);
	new victim = read_data(2);
	new hs = read_data(3);
	new weaponName[ 20 ]; 
	read_data( 4 , weaponName , charsmax( weaponName ) );
	new wpn = getWeaponByShortName(weaponName);
	
	addPlayerFrags(id, victim, wpn, hs);
	
	if(task_exists(TASK_INVISIBILITY + victim))
		remove_task(TASK_INVISIBILITY+victim);
		
	if(task_exists( TASK_GIVE_HE + victim))
		 remove_task(TASK_GIVE_HE + victim);
	if(gPlayerSettings[id][SETTINGS_SHOW_LVL_ON_KILL]){
		if(is_user_alive(id) && isLevelWeapon(wpn)){
			showWeaponReq(id, wpn);
		}
	}
	return PLUGIN_CONTINUE;
}

public client_death(killer, victim, wpn, hitplace, TK) {
	if(!isActive()) return PLUGIN_CONTINUE;
	if(!isLevelPowers()) return PLUGIN_CONTINUE;
	
	server_print("client_death(killer=%d, victim=%d, wpn=%d, hitplace=%d, TK=%d)", killer, victim, wpn, hitplace, TK);
	
	if (wpn == CSW_C4 && isPlayer(killer) && gPlayerLevelInfo[killer][WEAPONS_HEGRENADE] > LEVEL_EXPERT) {
		if(cs_get_user_team(victim)  == CS_TEAM_CT)
			addPlayerFrags(killer, victim, WEAPONS_HEGRENADE, 0);
	}
	
	if(task_exists(TASK_INVISIBILITY + victim))
		remove_task(TASK_INVISIBILITY+victim);
			
	if(task_exists( TASK_GIVE_HE + victim))
		 remove_task(TASK_GIVE_HE + victim);
		 
	return PLUGIN_CONTINUE;
}

public CurWeaponEvent(id) {
	if(!isActive()) return;
	if(!isLevelPowers()) return;
	if(!is_user_alive(id)) return;
	
	new wpn =  read_data(2) ;
	if (wpn == 0) return;
		
	//Set player visibiliti
	if(wpn == WEAPONS_KNIFE){
		if(!task_exists(TASK_INVISIBILITY + id)){
			set_task(1.0,"setPlayerVisibility", TASK_INVISIBILITY + id,"",0,"b");
		}
	} else {
		if(task_exists(TASK_INVISIBILITY + id)){
			remove_task(TASK_INVISIBILITY + id);
			fm_set_rendering(id);
		}
	}
	
	if(gPlayerSettings[id][SETTINGS_SHOW_LVL_ON_WPN_CHANGE]){
		if(is_user_alive(id) && isLevelWeapon(wpn)){
			showWeaponReq(id, wpn);
		}
	}
	
	//Set fire rate speed
	new speed = getFireSpeed(id);
	if(wpn!=  WEAPONS_C4){		
		if(speed !=0){
			new weaponName[32],ent;
			get_weaponname(wpn,weaponName,31);
			ent = fm_find_ent_by_owner(-1,weaponName,id);
			if(ent) {
				static Float:Delay,Float:M_Delay;
				Delay = get_pdata_float( ent, 46, 4);
				Delay = Delay - (Delay * (speed / 100.0));
				M_Delay = get_pdata_float( ent, 47, 4);
				M_Delay = M_Delay - (M_Delay * (speed / 100.0));
				if (Delay > 0.0) {
					set_pdata_float( ent, 46, Delay, 4);
					set_pdata_float( ent, 47, M_Delay, 4);
				}
			}
		}
	}
	
	//Set movement speed
	new zoom=cs_get_user_zoom(id);
	new Float:normalSpeed = 0.0;
	switch (zoom){
		case 2,3,4:normalSpeed =  getZoomWeaponSpeed(wpn);
		default: normalSpeed= getWeaponSpeed(wpn);
	}
	speed = getUserSpeed(id);
	if(speed){
		normalSpeed += (normalSpeed*(speed/100));
	} 
	set_pev(id,pev_maxspeed,normalSpeed);
	
}





public HamTakeDamage(victim, inflictor, attacker, Float:damage, bits){
	if(!isActive()) return HAM_IGNORED;
	if(!isLevelPowers()) return HAM_IGNORED;
	if(!isPlayer(attacker)) return HAM_IGNORED;
	if(!isPlayer(victim)) return HAM_IGNORED;
	
	new hitgroup = get_pdata_int(victim, OFFSET_LAST_HIT_GROUP, EXTRAOFFSET_PL_LINUX) ;
	new wpn = get_user_weapon(attacker);
	if (bits & (1<<24))
		wpn = WEAPONS_HEGRENADE;
		
	switch(wpn){
		case WEAPONS_P228,WEAPONS_ELITE,WEAPONS_FIVESEVEN,WEAPONS_DEAGLE,WEAPONS_USP,WEAPONS_GLOCK18:{
			new percent = getDropWeaponChance(attacker);
			if(percent){
				if(random_num(1, 100)<=percent){
					client_cmd(victim, "drop");
				}
			}
		}
	}

	new bool:hs  = false;
	if(hitgroup == HIT_HEAD)
		hs = true;
		
	new Float:newDmg = getPlayerDamageGive(attacker, wpn, damage, hs);
	if(newDmg>=1000.0){
		new health = get_user_health(victim);
		SetHamParamFloat(4, float(health));
		return HAM_IGNORED;
	}
	
	newDmg = getPlayerDamageReceive(victim, newDmg);
	if(newDmg != damage){
		SetHamParamFloat(4, newDmg);
		return HAM_IGNORED;
	}
	return HAM_IGNORED;
}


public setPlayerVisibility(taskid){
	new id = taskid - TASK_INVISIBILITY ;
	new percent = getPlayerVisibility(id, isPlayerMoving(id));
	if(percent != gPlayerVisibility[id]){
		gPlayerVisibility[id] = percent;
		if(percent != 100)
			fm_set_rendering(id, kRenderFxNone, 0,0,0, kRenderTransTexture,255 - 255 * (percent /100));
		else 
			fm_set_rendering(id);
	}
}


stock getRoundBestPlayer(const players[32], num) {
	new player;
	new best = 0;
	new roundXp = 0;
	new tmpXp;
	
	for(new i=0;i<num;i++) {
		player = players[i];
		tmpXp = gPlayerXp[player][T_ROUND];

		if(tmpXp > roundXp) {
			roundXp = tmpXp;
			best = player;
		}
	}
	return best;
}

public bombPlantingEvent(id){
	if(isShorterC4Timer(id)){
		set_pcvar_num(gPCvarC4Timer, gC4Timer - get_pcvar_num(gCvarShorterC4Time));
	} else {
		set_pcvar_num(gPCvarC4Timer, gC4Timer );
	}
}


public HamSpawn(id){
	if(!isActive()) return;
	if(!isLevelPowers()) return;
	
	new lvl = gPlayerLevelInfo[id][WEAPONS_HEGRENADE];
	switch(lvl){
		case LEVEL_EXPERT, LEVEL_OWNER: give_item(id, "weapon_hegrenade");
	}
	new gravity = getUserGravity(id);
	if(gravity){
		set_user_gravity(id, 1.0 - (float(gravity)/100.0));
	}
	new hp, armour;
	getUserHpAndArmour(id, hp, armour);
	if(hp){
		hp += get_user_health(id);
		set_user_health(id, hp);
	}
	if(armour){
		new CsArmorType:type;
		new old = cs_get_user_armor(id, type);
		if(armour > old){
			cs_set_user_armor(id, armour, CS_ARMOR_VESTHELM);
		}
	}
	
	lvl = getFreeScout(id);
	if(lvl){
		if(random_num(1, 100) <= lvl){
			give_item(id, "weapon_scout");
		}
	}
}

	
public grenade_throw ( id, greindex, wpn ) {
 	if(!isActive()) return;
	if(!isLevelPowers()) return;
	if(!is_user_alive(id)) return;
	server_print("grenade_throw %d, %d", greindex, wpn );
	if(wpn == WEAPONS_HEGRENADE){
		new lvl = gPlayerLevelInfo[id][WEAPONS_HEGRENADE];
		if(task_exists( TASK_GIVE_HE + id)){
			 remove_task(TASK_GIVE_HE + id);
		}
		switch(lvl){
			case LEVEL_EXPERT, LEVEL_OWNER: set_task(15.0, "giveHe", TASK_GIVE_HE + id);
		}
	}
}
 
public giveHe(taskid) {
	new id = taskid - TASK_GIVE_HE;
	if(!is_user_alive(id)) return;
	give_item(id, "weapon_hegrenade");
}
 


